//
//  BLInfoSliderCell.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/27.
//

#import "BLInfoSliderCell.h"

@implementation BLInfoSliderCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)reloadWithModel:(NSString *)title min:(NSInteger)minAge max:(NSInteger)maxAge curMin:(NSInteger)curMin curMax:(NSInteger)curMax unit:(NSString *)unit{
    self.minAge = minAge;
    self.maxAge = maxAge;
    self.curMinAge = curMin;
    self.curMaxAge = curMax;
    self.unit = unit;
    self.labelTitle.text = title;
    if (!_doubleSliderView) {
        [self.viewBj addSubview:self.doubleSliderView];
    }
    [self changeSliderValue];
    [self changeAgeTipsText];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (CGFloat)fetchIntFromValue:(CGFloat)value {
    CGFloat newValue = floorf(value);
    CGFloat changeValue = value - newValue;
    if (changeValue >= 0.5) {
        newValue = newValue + 1;
    }
    return newValue;
}
- (void)sliderValueChangeActionIsLeft: (BOOL)isLeft finish: (BOOL)finish {
    if (isLeft) {
        CGFloat age = (self.maxAge - self.minAge) * self.doubleSliderView.curMinValue;
        CGFloat tmpAge = [self fetchIntFromValue:age];
        self.curMinAge = (NSInteger)tmpAge + self.minAge;
        [self changeAgeTipsText];
    }else {
        CGFloat age = (self.maxAge - self.minAge) * self.doubleSliderView.curMaxValue;
        CGFloat tmpAge = [self fetchIntFromValue:age];
        self.curMaxAge = (NSInteger)tmpAge + self.minAge;
        [self changeAgeTipsText];
    }
    if (finish) {
        [self changeSliderValue];
        
    }
}

//值取整后可能改变了原始的大小，所以需要重新改变滑块的位置
- (void)changeSliderValue {
    CGFloat finishMinValue = (CGFloat)(self.curMinAge - self.minAge)/(CGFloat)(self.maxAge - self.minAge);
    CGFloat finishMaxValue = (CGFloat)(self.curMaxAge - self.minAge)/(CGFloat)(self.maxAge - self.minAge);
    self.doubleSliderView.curMinValue = finishMinValue;
    self.doubleSliderView.curMaxValue = finishMaxValue;
    [self.doubleSliderView changeLocationFromValue];
    if (self.myBlock) {
        self.myBlock([NSString stringWithFormat:@"%ld",self.curMinAge], [NSString stringWithFormat:@"%ld",self.curMaxAge]);
    }
}

- (void)changeAgeTipsText {
    self.doubleSliderView.minLabel.text =[NSString stringWithFormat:@"%li%@", self.curMinAge,self.unit?:@""];
    self.doubleSliderView.maxLabel.text =[NSString stringWithFormat:@"%li%@", self.curMaxAge,self.unit?:@""];
    [self.doubleSliderView.minLabel sizeToFit];
    [self.doubleSliderView.maxLabel sizeToFit];
}

- (DoubleSliderView *)doubleSliderView {
    if (!_doubleSliderView) {
        _doubleSliderView = [[DoubleSliderView alloc] initWithFrame:CGRectMake(53, 0, BL_WINDOW_WIDTH - 90, 60)];
        _doubleSliderView.needAnimation = true;
        _doubleSliderView.labelColor = [UIColor chColorWithHexString:@"#555555"];
//        CGFloat offset = self.maxAge - self.minAge;
//        if (offset > 4.0) {
//            _doubleSliderView.minInterval = 4.0/(offset);
//        }
        _doubleSliderView.minLabel.text =[NSString stringWithFormat:@"%li%@", self.curMinAge,self.unit?:@""];
        _doubleSliderView.maxLabel.text =[NSString stringWithFormat:@"%li%@", self.curMaxAge,self.unit?:@""];
        [_doubleSliderView.minLabel sizeToFit];
        [_doubleSliderView.maxLabel sizeToFit];
        __weak typeof(self) weakSelf = self;
        _doubleSliderView.sliderBtnLocationChangeBlock = ^(BOOL isLeft, BOOL finish) {
            [weakSelf sliderValueChangeActionIsLeft:isLeft finish:finish];
        };
    }
    return _doubleSliderView;
}

@end
